/**
 * @file main.js
 * @author admin
 * @description 主页面
 */
import API from "../../api/api.js";
var app = getApp();
Page({
    // 除了扫码查询之外
    // 点击任何按钮  先判断是否登录
    // 已登录继续
    // 未登录 先绑定
  onLoad (){
    var that = this;
    var user=wx.getStorageSync('user') || {};    
    var userInfo=wx.getStorageSync('userInfo') || {};   
    if((!user.openid || (Date.now()) < (Date.now() + 600))){   
        wx.login({    
        success: function(res){   
            if(res.code) {    
                console.log(res.code);
                var d=that.globalData;//这里存储了appid、secret、token串    
                var getOpenIdUrl ='https://api.weixin.qq.com/sns/jscode2session?appid='+d.appid+'&secret='+d.secret+'&js_code='+res.code+'&grant_type=authorization_code';    
                wx.request({    
                    url: getOpenIdUrl,    
                    data: {},    
                    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT    
                    // header: {}, // 设置请求的 header    
                    success: function(res){   
                        var obj = res.data;    
                        console.log(obj);  
                        wx.setStorageSync('user', obj);//存储openid    
                    }    
                }); 
                wx.request({
                  url: app.globalData.domain + '/userInfo/getUser', //仅为示例，并非真实的接口地址
                  data: {
                    openid: wx.getStorageSync('user').openid,
                    code: res.code
                  },
                  header: {
                    'content-type': 'application/json' // 默认值
                  },
                  success (res) {
                    var data = res.data;
                    if(data.code == 200) {
                      var userInfo = data.data;
                      console.log(userInfo);
                      wx.setStorageSync('userInfo', userInfo)
                    }
                  }
                });
            }else {  
                console.log('获取用户登录态失败！' + res.errMsg)  
            }            
        }    
      });   
    }   
  },
  scan() {
    //扫码事件
    wx.scanCode({
      success: function(res) {
        wx.navigateTo({
          url: "../scan/scan"
        })
      },
      fail: function(res) {
        wx.showToast({
          title: '扫码失败',
          image: '../../images/failmsg.png',
          duration: 2000
        })
      }
    })
  }, 
  bind() {
    wx.navigateTo({
      url: "../bind/bind"
    })
  },
  remote() {
    var userInfo = wx.getStorageSync("userInfo");
    if(null == userInfo){
      wx.navigateTo({
        url: "../bind/bind"
      })
    } else {
      wx.navigateTo({
        url: "../remote/remote"
      });
    }
  },
  mine() {
    var userInfo = wx.getStorageSync("userInfo");
    if(null == userInfo){
      wx.navigateTo({
        url: "../bind/bind"
      })
    } else {
      wx.switchTab({
        url: "../user/user"
      })
    }
  },
  globalData:{ 
    appid:'wx35c629d3f32531fb',
    secret:'25a4a651d481717975617d76c1578794',
  },  
})
